﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow;
using System.Windows.Media.DirectShow.Filters;
using System.Windows.Media.DirectShow.Ogg;
using System.Windows.Media.DirectShow.UI;

namespace Demo
{
    public partial class WavePage : Page
    {

        private FilterGraph graph;
        private VideoAudioRenderer videoAudioRenderer;
        private WaveSourceParser sourceFilter;
        private AudioVisualization audioVisualization;

        public WavePage()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            graph = new FilterGraph();

            videoAudioRenderer = new VideoAudioRenderer();
            graph.AddFilter("render", videoAudioRenderer);

            sourceFilter = new WaveSourceParser();
            sourceFilter.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(sourceFilter_LoadCompleted);
            graph.AddFilter("source", sourceFilter);

            BarVisualization visualization = new BarVisualization();
            visualization.Width = 300;
            visualization.Height = 200;
            visualization.HorizontalAlignment = HorizontalAlignment.Center;

            audioVisualization = new AudioVisualization(visualization);
            graph.AddFilter("av", audioVisualization);

            gv.SetGraph(graph, new Uri("test1.wav", UriKind.Relative));
        }

        void sourceFilter_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                graph.ConnectFilters(sourceFilter, audioVisualization, false);
                graph.ConnectFilters(audioVisualization, videoAudioRenderer, false);
            }
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);

            graph.StopAsync();
        }
    }
}